﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model;
using Cn.Edwon.Web;
using Cn.Edwon.Web.Validation;
using Cn.Cellula.Model.Security;
using Cn.Cellula.Client;
namespace Cn.Cellula.Website.Admin.Permission.Func {
    public partial class Edit : PageSupport {
        String Key = null;
        String ParentKey = null;
        IUserBiz biz = BizFactory.Get<IUserBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            Key = Request.QueryString["id"];
            ParentKey = Request.QueryString["pid"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(Key)) {
                    FillForm(Key);
                } else {
                    if (!String.IsNullOrEmpty(ParentKey))
                        this.ddl_Parent.SelectedValue = ParentKey;
                }
            }
        }
        private void InitForm() {
            this.ddl_Parent.DataSource = biz.LoadFunc2ListItem(this.Actor,"..", Key, false);
            this.ddl_Parent.DataValueField = "Value";
            this.ddl_Parent.DataTextField = "Text";
            this.ddl_Parent.DataBind();
        }
        private void FillForm(String key) {
            TSysFunctionEO eo = biz.Load<TSysFunctionEO>(key);
            this.txt_MenuName.Text = eo.MenuName;
            this.txt_Name.Text = eo.FuncName;
            this.ddl_Parent.SelectedValue = eo.ParentKey;
            this.txt_OrderNubmer.Text = eo.OrderNumber + "";
            this.txt_Pattern.Text = eo.MarkPattern;
            this.txt_Url.Text = eo.Url;
            this.cbx_FlgOrgOpen.Checked = (eo.FlgOrgOpen==1);
        }
        private void BindData2EO(ref TSysFunctionEO eo) {
            eo.FuncName = this.txt_Name.Text;
            eo.MarkPattern = this.txt_Pattern.Text;
            eo.MenuName = this.txt_MenuName.Text;
            eo.ParentKey = this.ddl_Parent.SelectedValue;
            eo.Url = this.txt_Url.Text;
            try {
                eo.OrderNumber = Convert.ToInt32(this.txt_OrderNubmer.Text);
            } catch {
                eo.OrderNumber = 0;
            }
            eo.FlgOrgOpen = (this.cbx_FlgOrgOpen.Checked) ? 1 : 0;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_MenuName.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            TSysFunctionEO eo = new TSysFunctionEO();
            if (String.IsNullOrEmpty(Key)) {
                BindData2EO(ref eo);
                biz.Save<TSysFunctionEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("功能", eo.FuncName, this.Actor, Request);
            } else {//update
                eo = biz.Load<TSysFunctionEO>(Key);
                BindData2EO(ref eo);
                biz.Update<TSysFunctionEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("功能", eo.FuncName, this.Actor, Request);
            }
            Response.Redirect("List.aspx");
            //SiteInfo.SendSucceed(Page, "List.aspx?f=" + Key);
        }
    }
}
